<?php
//No direct access
defined('_JEXEC') or die('Restricted access');
class MenuHelper
{
	function nav()
	{
		global $mainframe, $option;

		//Initialise variables
		$db			=& JFactory::getDBO();
		$document	=& JFactory::getDocument();
		$user		=& JFactory::getUser();
		$ownuser	=& Ownservice::getUser();

		// Get vars
		$gid		= $user->get('gid');
		$level		= $ownuser->get('level_id');
		$level		= ($gid < 25)?$level:6;
		$priceformat= OwnserviceHelper::getParams('price_format', '#,###.##');
		$dateformat = OwnserviceHelper::getParams('date_format', '%d/%m/%Y');
		$path		= JURI::base().'/components/com_ownservice/assets/images';

		// Add css to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/menu.min.css');
		$document->addStyleDeclaration("
										#menu_ownservice A{ 
											font-size: 12px !important; 
											font-weight: normal !important; 
											text-transform: none !important; 
										}");
		// Add js to document
		$document->addScript(JURI::base().'/components/com_ownservice/assets/js/ext-core.js');
		$document->addScript(JURI::base().'/components/com_ownservice/assets/js/menu.min.js');
		$document->addScriptDeclaration('
											Ext.onReady(function() { 
												new Ext.ux.Menu("menu_ownservice", {"transitionType":"slide","transitionDuration":0.2}); 
											});
										');

		# Link
		$link_home						= JRoute::_('index.php?option=com_ownservice');
		$link_today_transaction			= JRoute::_('index.php?option=com_ownservice&view=transactions');
		$link_customer_transfers		= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=customertransfers');
		$link_transferring_transaction 	= JRoute::_('index.php?option=com_ownservice&view=transactions&layout=receivingtransfer');
		$link_transferring				= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=transferring');
		$link_transaction				= JRoute::_('index.php?option=com_ownservice&view=transactions');
		$link_tranar					= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=paidtransactions');
		//$link_trana					= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=requestedapproval');
		//$link_tranat					= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=approvedtransactions');
		$link_reapproval				= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=requestapproval');
		$link_reapproval_cor			= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=requestapprovalcorrected');
		$link_failed					= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=failedtransactions');
		$link_cancelled					= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=cancelledtransactions'); 
		$link_corrected					= JRoute::_('index.php?option=com_ownservice&controller=transaction&task=correctedtransactions');
		$link_transactionall			= JRoute::_('index.php?option=com_ownservice&view=transactionall');
		$link_account					= JRoute::_('index.php?option=com_ownservice&view=accounts');
		$link_accountadd				= JRoute::_('index.php?option=com_ownservice&controller=account&task=add');
		$link_members					= JRoute::_('index.php?option=com_ownservice&view=members');
		$link_users						= JRoute::_('index.php?option=com_ownservice&view=users');
		$link_history					= JRoute::_('index.php?option=com_ownservice&view=history');
		$link_transfer_history			= JRoute::_('index.php?option=com_ownservice&view=transferhistories');
		$link_exchangerate				= JRoute::_('index.php?option=com_ownservice&view=showexchangerate');
		$link_rate						= JRoute::_('index.php?option=com_ownservice&view=exchangerates');
		$link_currency					= JRoute::_('index.php?option=com_ownservice&view=currency');
		//$link_fee						= JRoute::_('index.php?option=com_ownservice&view=fees');
		$link_printreport				= JRoute::_('index.php?option=com_ownservice&view=reporttransfer');
		$link_report					= JRoute::_('index.php?option=com_ownservice&view=report');
		$link_report_user				= JRoute::_('index.php?option=com_ownservice&controller=report&task=user');
		$link_report_st					= JRoute::_('index.php?option=com_ownservice&controller=report&task=stafftransfer');
		$link_branch					= JRoute::_('index.php?option=com_ownservice&view=branches');
		$link_vip 						= JRoute::_('index.php?option=com_ownservice&view=levels');
		$link_point 					= JRoute::_('index.php?option=com_ownservice&view=points');
		$link_setting					= JRoute::_('index.php?option=com_ownservice&view=settings');
		$link_logout					= JRoute::_('index.php?option=com_user&task=logout&return='.base64_encode($link_home));

		// Member Menu
		if( ($gid < 25) && ($level < 2) )
		{
			// add bootstrap to document
			$document->addStyleSheet(JURI::base(true).'/components/com_ownservice/assets/bootstrap-3.1.1/css/bootstrap.min.css');

			// add script to document
			$document->addScript(JURI::base(true).'/components/com_ownservice/assets/js/jquery-1.7.2.min.js');
			$document->addScript(JURI::base(true).'/components/com_ownservice/assets/bootstrap-3.1.1/js/bootstrap.min.js');

			$menu = '<ul id="menu_ownservice" class="ux-menu ux-menu-horizontal">';
			$menu .= '
					<li class="ux-menu-item-main">
						<a href="'.$link_home.'" class="ux-menu-link-first">
							<span><img src="'.$path.'/home.png" align="absmiddle"> '.JText::_('OWNSERVICE_MAIN_PAGE').'</span>
						</a>
					</li>
					';
			$menu .='
					<li class="ux-menu-item-main">
						<a href="'.$link_history.'">
							<span><img src="'.$path.'/id_card.png" align="absmiddle"> '.JText::_('OWNSERVICE_RECENT_TRANSACTIONS').'</span>
						</a>

					</li>
					';
			$menu .='
					<li class="ux-menu-item-main">
						<a href="'.$link_exchangerate.'">
							<span><img src="'.$path.'/dollar_currency_sign.png" align="absmiddle"> '.JText::_('OWNSERVICE_EXCHANGERATES').'</span>
						</a>
					</li>
					';
			$menu .='
					<li class="ux-menu-item-main">
						<a href="'.$link_logout.'">
							<span><img src="'.$path.'/id_card.png" align="absmiddle"> '.JText::_('OWNSERVICE_LOGOUT').'</span>
						</a>

					</li>
					';
			$menu .= '</ul>';

			//Get profile
			$query = ' SELECT p.*, u.name, u.username FROM #__ownservice_users AS p '
					.' LEFT JOIN #__users AS u ON u.id=p.user_id '
					.' WHERE u.id='.$user->get('id').' AND u.block = 0 ';

			$db->setQuery($query);
			$row = $db->loadObject();

			$expiredate = JHTML::_('date', $row->expire_date, $dateformat);
			$expireA	= explode('-', $row->expire_date);
			$cdate		= mktime(0, 0, 0, date('m'), date('d'), date('Y'));
			$exdate		= mktime(0, 0, 0, $expireA[1], $expireA[2], $expireA[0]);
			$date		= ($exdate-$cdate)/86400;
			$style = '';
			if( ($date > 7) && ($date <= 30) ){
				$style = 'color:#F2E209';
			} else if($date <= 7) {
				$style = 'color:#ED0924';
			}
			$menu .='<div class="clear"> </div>';
			$menu .='<table width="100%">';
			$menu .='<tr>';
				$menu .='<td>';
					$menu .='<div style="margin-top:10px;">';
					$menu .=JText::_('OWNSERVICE_WELCOME').' : <strong>'.$user->get('name').'</strong> ';
					$menu .=' | '.JText::_('OWNSERVICE_MEMBER_ID').' : <strong>'.$user->get('username').'</strong>';
					$menu .=' | '.JText::_('OWNSERVICE_ID_TYPE').' : <strong>'.$row->id_type.'</strong>';
					$menu .=' | '.JText::_('OWNSERVICE_ID_NUMBER').' : <strong>'.$row->id_number.'</strong>';
					$menu .=' | '.JText::_('OWNSERVICE_EXPIRE_DATE').' : <span style="'.$style.'"><strong>'.$expiredate.'</strong></span>';
					$menu .=' | '.JText::_('OWNSERVICE_POINT').' : <strong>'.OwnserviceHelper::getPoint($user->get('id')).'</strong> ';
					$menu .='</div>';
				$menu .='</td>';
			$menu .='</tr>';
			$menu .='<tr>';
				$menu .='<td>';
				$menu .='<table>';
				$menu .='<td>';
					$menu .=JText::_('OWNSERVICE_LEVEL').' : '.OwnserviceHelper::getUserLevel($user->get('id'));
				$menu .='</td>';
				$menu .='<td style="width: 100px;">';
					$menu .='<div style="margin-top:10px;">';
						$menu .='<div class="progress progress-striped">';
							$menu .='<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: '.OwnserviceHelper::getPercentLevel().'%">';
								$menu .='<span class="sr-only">'.OwnserviceHelper::getPercentLevel().'% Complete (success)</span>';
							$menu .='</div>';
						$menu .='</div>';
					$menu .='</div>';
				$menu .='</td>';
				$menu .='<td>'.OwnserviceHelper::getPercentLevel().'/100 %</td>';
				$menu .='</table>';
				$menu .='</td>';
			$menu .='</tr>';
			$menu .='</table>';
			echo $menu;
		}

		// Admin Menu
		if( ($gid <= 25) && ($level > 1) )
		{
			$menu = '<ul id="menu_ownservice" class="ux-menu ux-menu-horizontal">';
			$menu .= '
					<li class="ux-menu-item-main">
						<a href="'.$link_home.'" class="ux-menu-link-first">
							<span><img src="'.$path.'/home.png" align="absmiddle"> '.JText::_('OWNSERVICE_MAIN_PAGE').'</span>
						</a>
					</li>
					';
			
			//check access transactions
			if( ($gid == 25) || (Ownservice::checkAccess('transaction','manage')) ) 
			{
				//access
				$canAR		= ($gid < 25)?Ownservice::checkAccess('transaction', 'approval', 'transaction', 'all'):1;
				$canAT		= ($gid < 25)?Ownservice::checkAccess('transaction', 'transfer', 'transaction', 'all'):1;
				$canVEXC	= ($gid < 25)?Ownservice::checkAccess('showexchangerate', 'view', 'showexchangerate', 'all'):1;
				$canF		= ($gid < 25)?Ownservice::checkAccess('transaction', 'failed', 'transaction', 'all'):1;
				$canC		= ($gid < 25)?Ownservice::checkAccess('transaction', 'cancelled', 'transaction', 'all'):1;
				$canCor		= ($gid < 25)?Ownservice::checkAccess('transaction', 'corrected', 'transaction', 'all'):1;
				$canHistory = ($gid < 25)?Ownservice::checkAccess( 'history', 'manage' ):1;
				$canTR		= ($gid < 25)?Ownservice::checkAccess('transaction', 'transferring', 'transaction', 'all'):1;
				$canTA		= ($gid < 25)?Ownservice::checkAccess('transaction', 'transactionall', 'transaction', 'all'):1;
				
				$menu .='
						<li class="ux-menu-item-main ux-menu-item-parent">
							<a href="javascript:void(0);" class="ux-menu-link-parent">
								<span><img src="'.$path.'/refresh.png" align="absmiddle">'.JText::_('OWNSERVICE_TRANSACTIONS').'</span>
							</a>
						';
						
				$menu .='<ul class="ux-menu-sub ux-menu-init-hidden">';
				
				if($level != 3)
				{
					$menu .='<li>
								<a href="'.$link_today_transaction.'" class="ux-menu-link-first">
									<span><img src="'.$path.'/refresh.png" align="absmiddle">'.JText::_('OWNSERVICE_TODAYS_TRANSACTIONS').'</span>
								</a>
							</li>';

					$menu .='<li>
									<a href="'.$link_customer_transfers.'" class="ux-menu-link-first">
										<span><img src="'.$path.'/users.png" align="absmiddle"> '.JText::_('OWNSERVICE_CUSTOMER_TRANSACTIONS').'</span>
									</a>
								  </li>
								';
				} 
				else 
				{
					$menu .='<li>
								<a href="'.$link_transferring_transaction.'" class="ux-menu-link-first">
									<span><img src="'.$path.'/refresh.png" align="absmiddle">'.JText::_('OWNSERVICE_TRANSFERRING_TRANSACTIONS').'</span>
								</a>
							</li>';
				}
				
				//Failed Transactions	
				if($canF)
				{
					$failed = MenuHelper::getTotalFailedTransactions();
					if($failed) {
						$menu .='<li>
									<a href="'.$link_failed.'">
										<span><img src="'.$path.'/failed_transfer.png" align="absmiddle"> <strong>'.JText::_('OWNSERVICE_FAILED_TRANSACTIONS').' ('.$failed.')</strong></span>
									</a>
								 </li>
								';
					} else {
						$menu .='<li>
										<a href="'.$link_failed.'">
											<span><img src="'.$path.'/failed_transfer.png" align="absmiddle"> '.JText::_('OWNSERVICE_FAILED_TRANSACTIONS').'</span>
										</a>
									 </li>
									';
					}
				}

				//Approval Transactions				
				if( $canAR ) {
					$menu .='<li>
								<a href="'.$link_tranar.'">
									<span><img src="'.$path.'/approval_request.png" align="absmiddle">'.JText::_('OWNSERVICE_PAID_TRANSACTIONS').'</span>
								</a>
							 </li>
							';
				}

				//Corrected Transactions
				if($canCor)
				{
					$correct = MenuHelper::getTotalCorrectTransactions();
					
					if($correct) {
							$menu .='<li>
								<a href="'.$link_corrected.'">
									<span><img src="'.$path.'/corrected_transactions.png" align="absmiddle"> <strong>'.JText::_('OWNSERVICE_CORRECTED_TRANSACTIONS').' ('.$correct.')</strong></span>
								</a>
							 </li>
							';
					} else {
							$menu .='<li>
								<a href="'.$link_corrected.'">
									<span><img src="'.$path.'/corrected_transactions.png" align="absmiddle"> '.JText::_('OWNSERVICE_CORRECTED_TRANSACTIONS').'</span>
								</a>
							 </li>
							';
					}
				}
				
				//Request Approval AND (Corrected)
				if( $canAT ) {
					$rp	= MenuHelper::getTotalRequestApproval();
					
					if($rp) {
						$menu .='<li>
									<a href="'.$link_reapproval.'">
										<span><img src="'.$path.'/approval_transferred.png" align="absmiddle"> <strong>'.JText::_('OWNSERVICE_REQUEST_APPROVEL').' ('.$rp.')</strong></span>
									</a>
								 </li>
								';
					} else {
						$menu .='<li>
									<a href="'.$link_reapproval.'">
										<span><img src="'.$path.'/approval_transferred.png" align="absmiddle"> '.JText::_('OWNSERVICE_REQUEST_APPROVEL').'</span>
									</a>
								 </li>
								';
					}
					
					$rpc = MenuHelper::getTotalRequestapprovalcorrected();	
					if( $rpc ) {	
						$menu .='<li>
									<a href="'.$link_reapproval_cor.'">
										<span><img src="'.$path.'/approval_transfer.png" align="absmiddle"> <strong>'.JText::_('OWNSERVICE_APPROVAL_PENDING_CORRECTED').' '.$rpc.'</strong></span>
									</a>
								 </li>
								';
					} else {
						$menu .='<li>
									<a href="'.$link_reapproval_cor.'">
										<span><img src="'.$path.'/approval_transfer.png" align="absmiddle"> '.JText::_('OWNSERVICE_APPROVAL_PENDING_CORRECTED').'</span>
									</a>
								 </li>
								';
					}
				}
		
				//Cancelled Transactions
				if($canC)
				{
					$cancel = MenuHelper::getTotalCancelTransactions();
					
					if($cancel) {
						$menu .='<li>
										<a href="'.$link_cancelled.'">
											<span><img src="'.$path.'/cancelled_transfer.png" align="absmiddle"> <strong>'.JText::_('OWNSERVICE_CANCELLED_TRANSACTIONS').' ('.$cancel.')</strong></span>
										</a>
									 </li>
									';	
					} else {
						$menu .='<li>
										<a href="'.$link_cancelled.'">
											<span><img src="'.$path.'/cancelled_transfer.png" align="absmiddle"> '.JText::_('OWNSERVICE_CANCELLED_TRANSACTIONS').'</span>
										</a>
									 </li>
									';
					}
				}

				//Transferring Transactions
				if($canTR)
				{
						$menu .='<li>
										<a href="'.$link_transferring.'">
											<span><img src="'.$path.'/refresh.png" align="absmiddle">'.JText::_('OWNSERVICE_TRANSFERRING_TRANSACTIONS').'</span>
										</a>
									 </li>
									 ';
				}

				//Transaction All
				if($canTA)
				{
					$menu .='<li>
									<a href="'.$link_transactionall.'">
										<span><img src="'.$path.'/transfer_left_right.png" align="absmiddle" />'.JText::_('OWNSERVICE_TRANSACTIONS_ALL').'</span>
									</a>
								</li>';
				}
				
					$menu .='</ul>';
				$menu .='
						</li>
						';
			}

			// history
			if( $canHistory && ($level == 3) ) {
				$menu .='
						<li class="ux-menu-item-main">
							<a href="'.$link_transfer_history.'">
								<span><img src="'.$path.'/id_card.png" align="absmiddle"> '.JText::_('OWNSERVICE_TRANSFER_MONEY_HISTORY').'</span>
							</a>
	
						</li>
						';
			}	
				
			//check access accounts
			if( ($gid == 25) || (Ownservice::checkAccess('account','manage')) ) 
			{
				$menu .='
					<li class="ux-menu-item-main">
						<a href="'.$link_account.'">
							<span><img src="'.$path.'/id_card.png" align="absmiddle"> '.JText::_('OWNSERVICE_RECEIVER_ACCOUNTS').'</span>
						</a>
					</li>
					';
				
			}
		
			if($canVEXC)
			{	
			//show exchange rate
			$menu .='
					<li class="ux-menu-item-main">
						<a href="'.$link_exchangerate.'">
							<span><img src="'.$path.'/dollar_currency_sign.png" align="absmiddle"> '.JText::_('OWNSERVICE_EXCHANGERATES').'</span>
						</a>
					</li>
					';
			}

			//check access user and memner
			if( ($gid == 25) || (Ownservice::checkAccess('member','manage')))
			{
				//access
				$canMM	=($gid < 25)?Ownservice::checkAccess('member','manage'):1;
				$canMU	=($gid < 25)?Ownservice::checkAccess('user','manage'):1;

				$menu .='
					<li class="ux-menu-item-main ux-menu-item-parent">
						<a href="'.$link_members.'" class="ux-menu-link-parent">
							<span><img src="'.$path.'/users.png" align="absmiddle"> '.JText::_('OWNSERVICE_USERS').'</span>
						</a>
					';
				if($canMM || $canMU) {
				$menu .='<ul class="ux-menu-sub ux-menu-init-hidden">';
					if($canMM) {
						$menu .='<li>
									<a href="'.$link_members.'" class="ux-menu-link-first">
										<span><img src="'.$path.'/users.png" align="absmiddle"> '.JText::_('OWNSERVICE_MEMBERS').'</span>
									</a>
								 </li>
								';
					}
					if($canMU) {
						$menu .='<li>
									<a href="'.$link_users.'">
										<span><img src="'.$path.'/users.png" align="absmiddle"> '.JText::_('OWNSERVICE_USERS').'</span>
									</a>
								 </li>
								';
					}
				$menu .='</ul>';
				}
				$menu .='
					</li>
					';
			}

			// check access report
			if( ($user->gid == 25) || (Ownservice::checkAccess('report','manage')) || (Ownservice::checkAccess('printreport','manage')) )
			{
				$menu .='
					<li class="ux-menu-item-main ux-menu-item-parent">
						<a href="#" class="ux-menu-link-parent">
							<span><img src="'.$path.'/chart.png" align="absmiddle"> '.JText::_('OWNSERVICE_REPORTS').'</span>
						</a>
					';
				
				$menu .='<ul class="ux-menu-sub ux-menu-init-hidden">';
					
					if( (Ownservice::checkAccess('printreport','manage')) )
					{
						$menu .='<li>
									<a href="'.$link_printreport.'" class="ux-menu-link-first">
										<span><img src="'.$path.'/refresh.png" align="absmiddle"> '.JText::_('OWNSERVICE_TRANSFER_REPORT').'</span>
									</a>
								</li>
								';
					}

					if( ($user->gid == 25) || (Ownservice::checkAccess('report','manage')))
					{
						$menu .='<li>
									<a href="'.$link_report.'" class="ux-menu-link-first">
										<span><img src="'.$path.'/refresh.png" align="absmiddle"> '.JText::_('OWNSERVICE_TRANSFERRED_RECORD_ALL').'</span>
									</a>
								</li>
								';
						$menu .='<li>
									<a href="'.$link_report_user.'">
										<span><img src="'.$path.'/users.png" align="absmiddle"> '.JText::_('OWNSERVICE_TRANSFERRED_RECORD_USER').'</span>
									</a>
								</li>
								';
						$menu .='<li>
									<a href="'.$link_report_st.'">
										<span><img src="'.$path.'/users.png" align="absmiddle"> '.JText::_('OWNSERVICE_TRANSFERRED_RECORD_STAFF_TRANSFER').'</span>
									</a>
								</li>
								';
					}
				$menu .='</ul>';
				$menu .='
					</li>
					';
			}

			$exchangerate	= Ownservice::checkAccess('exchangerate','manage');
			$currency		= Ownservice::checkAccess('currency', 'manage');
			$fee			= Ownservice::checkAccess('fee','manage');
			$branch			= Ownservice::checkAccess('branch','manage');
			$vip 			= Ownservice::checkAccess('vip', 'manage');
			$point 			= Ownservice::checkAccess('point', 'manage');
			$setting		= Ownservice::checkAccess('setting','manage');

			if( ($gid == 25) || $currency || $exchangerate || $fee || $branch || $setting) 
			{
				
			//check access config
			$menu .='
				<li class="ux-menu-item-main ux-menu-item-parent">
					<a href="#" class="ux-menu-link-parent">
						<span><img src="'.$path.'/setting.png" align="absmiddle"> '.JText::_('OWNSERVICE_SETTINGS').'</span>
					</a>
				';
				$menu .='<ul class="ux-menu-sub ux-menu-init-hidden">';

				// check access currency
				if( ($gid == 25) || ($exchangerate))
				{
					$menu .='<li>
								<a href="'.$link_rate.'" class="ux-menu-link-first">
									<span><img src="'.$path.'/dollar_currency_sign.png" align="absmiddle"> '.JText::_('OWNSERVICE_EXCHANGERATES').'</span>
								</a>
							</li>
							';
				}

				// check access currency
				if( ($gid == 25) || ($currency))
				{
					$menu .='<li>
								<a href="'.$link_currency.'">
									<span><img src="'.$path.'/dollar_currency_sign.png" align="absmiddle"> '.JText::_('OWNSERVICE_CURRENCIES').'</span>
								</a>
							</li>
							';
				}

				// check access branch
				if( ($user->gid == 25) || ($branch) ) 
				{
					$menu .='<li>
								<a href="'.$link_branch.'">
									<span><img src="'.$path.'/branches.png" align="absmiddle"> '.JText::_('OWNSERVICE_BRANCHES').'</span>
								</a>
							</li>
							';
				}

				// check access vip
				if( ($user->gid ==25) || ($vip) )
				{
					$menu .='<li>
								<a href="'.$link_vip.'">
									<span><img src="'.$path.'/vip.png" align="absmiddle"> '.JText::_('OWNSERVICE_VIP').'</span>
								</a>
							</li>';
				}

				// check access point
				if( ($user->gid ==25) || ($point) )
				{
					$menu .='<li>
								<a href="'.$link_point.'">
									<span><img src="'.$path.'/earn.png" align="absmiddle"> '.JText::_('OWNSERVICE_POINT').'</span>
								</a>
							</li>';
				}

				// check access setting
				if( ($user->gid == 25) || ($setting)) 
				{
					$menu .='<li>
								<a href="'.$link_setting.'">
									<span><img src="'.$path.'/setting.png" align="absmiddle"> '.JText::_('OWNSERVICE_SETTINGS').'</span>
								</a>
							</li>
							';
				}
				$menu .='</ul>';
			$menu .='
				</li>
				';
			}

			// log out
			$menu .='
					<li class="ux-menu-item-main">
						<a href="'.$link_logout.'">
							<span><img src="'.$path.'/id_card.png" align="absmiddle"> '.JText::_('OWNSERVICE_LOGOUT').'</span>
						</a>

					</li>
					';
			$menu .= '</ul>';
			$menu .='<div class="clear"> </div>';
			$menu .='<div style="margin-top:10px;">';
			$menu .=JText::_('OWNSERVICE_WELCOME').' : <strong>'.$user->get('name').'</strong> ';
			$menu .=' | '.JText::_('OWNSERVICE_USERNAME').' : <strong>'.$user->get('username').'</strong>';
			$menu .='</div>';
	
			echo $menu;
		}
	}

	/**
	 * 
	 */
	 function getTotalPaidTransactions()
	 {
	 	global $mainframe, $option;
		
		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$db			= & JFactory::getDbo();

		//get vars
		$gid		= $user->gid;
		$level		= $ownuser->get('level_id');
		
		$filter_branch	= $mainframe->getUserStateFromRequest($option.'.paidtransactions.filter_branch','filter_branch',0,'int' );
		$search			= $mainframe->getUserStateFromRequest($option.'.paidtransactions.search', 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);
		
		$where		= array();
		$where[]	= 't.state = 1 ';

		//check access 
		if( ($gid < 25) &&  ($level < 5) ) {
			if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$ownuser->get('branch_id');
			}
		} else {
			//filter branch
			if($filter_branch) {
				$where[] = 't.branch_id ='.$filter_branch;
			}
		}
		
		//filter search
		if($search) {
			$where[]  =  ' ( t.invoice LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).' OR '
						.' t.account LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).')';
		}

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		//Query to retrieve all categories that belong under the web links saction and that are published.
		$query = ' SELECT t.*, p.id AS memberid, p.birthday, '
				.' u.name, u.username, b.name AS branchname, '
				.' c.iscore, a.bank_name, a.branch_city '
				.' FROM #__ownservice_transactions AS t '
				.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
				.' LEFT JOIN #__users AS u ON u.id = t.user_id '
				.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
				.' LEFT JOIN #__ownservice_branches AS b ON b.id = p.branch_id '
				.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id = t.rate_id '
				.' LEFT JOIN #__ownservice_currencies AS c ON c.id = r.currency_id '
				.$where
				.' GROUP BY t.id '
				.' ORDER BY t.requested_date DESC';
				
		$db->setQuery($query);
		$rows = $db->loadObjectList();

		return count($rows);	
	 }
	 
	/**
	 * Method to get Total Failed Transactions
	 *
	 *
	 * @return int
	 * @since 1.0
	*/
	function getTotalFailedTransactions()
	{
		global $mainframe, $option;

		//Initialise variables
		$user	= & JFactory::getUser();
		$ownuser= & Ownservice::getUser();
		$db = &JFactory::getDbo();
		
		//get vavrs
		$gid	= $user->gid;
		$level	= $ownuser->get('level_id');

		//filter 
		$filter_branch	= $mainframe->getUserStateFromRequest($option.'.failedtransaction.filter_branch','filter_branch',0,'int' );
		$filter_order	= $mainframe->getUserStateFromRequest($option.'.failedtransaction.filter_order', 'filter_order', 't.invoice', 'cmd');;
		$filter_order_dir= $mainframe->getUserStateFromRequest($option.'.failedtransaction.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');
		$search			= $mainframe->getUserStateFromRequest($option.'.failedtransaction.search', 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$where		= array();
		$where[]	= 't.state = '.$db->Quote('5');
		
		//branch
		if($filter_branch)
		{
			$where[] = 't.branch_id='.$filter_branch;	
		}
		
		//check access 
		if( ($gid < 25) && ($level < 5)) {
			if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$ownuser->get('branch_id');
			}
		} else {
			//filter branch
			if($filter_branch)
			{
				$where[] = 't.branch_id='.$filter_branch;	
			}
		}

		//filter search
		if($search) {
			$where[]  =  ' ( t.invoice LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).' OR '
						.' t.account LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).')';
		}
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_dir.', t.created';

		//Query to retrieve all categories that belong under the web links saction and that are published.
		$query = ' SELECT t.*,  '
					.' u.username, p.id AS memberid, a.bank_name, a.branch_city, '
					.' c.iscore '
					.' FROM #__ownservice_transactions AS t '
					.' LEFT JOIN #__users AS u ON u.id = t.user_id '
					.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
					.' LEFT JOIN #__ownservice_branches AS b ON b.id = p.branch_id '
					.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
					.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id = t.rate_id '
					.' LEFT JOIN #__ownservice_currencies AS c ON c.id = r.currency_id '
					.$where
					.' GROUP BY t.id '
					.$orderby
					;

		$db->setQuery($query);
		$rows = $db->loadObjectList();

		return count($rows);
	}

	function getTotalCancelTransactions()
	{
		global $mainframe, $option;

		//Initialise variables
		$user			= & JFactory::getUser();
		$ownuser		= & Ownservice::getUser();
		$db				= & JFactory::getDbo();

		//get vavrs
		$gid			= $user->gid;
		$level			= $ownuser->get('level_id');

		//filter 
		$filter_branch		= $mainframe->getUserStateFromRequest($option.'.cancelledtransaction.filter_branch','filter_branch',0,'int' );
		$filter_order		= $mainframe->getUserStateFromRequest($option.'.cancelledtransaction.filter_order', 'filter_order', 't.invoice', 'cmd');;
		$filter_order_dir	= $mainframe->getUserStateFromRequest($option.'.cancelledtransaction.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');
		$search				= $mainframe->getUserStateFromRequest($option.'.cancelledtransaction.search', 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$where			= array();
		$where[]		= 't.state = '.$db->Quote('8');
		$where[]		= 't.iscancel=0';
		
		//check access 
		if( ($gid < 25) && ($level < 5) ) {
			if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$ownuser->get('branch_id');
			}
		} else {
			//filter branch
			if($filter_branch)
			{
				$where[] = 't.branch_id='.$filter_branch;	
			}
		}
		
		//filter search
		if($search) {
			$where[]  =  ' ( t.invoice LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).' OR '
						.' t.account LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).')';
		}

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_dir.', t.created';

		//Query to retrieve all categories that belong under the web links saction and that are published.
		$query = ' SELECT t.*,  '
					.' u.username, p.id AS memberid, a.bank_name, a.branch_city, '
					.' c.iscore '
					.' FROM #__ownservice_transactions AS t '
					.' LEFT JOIN #__users AS u ON u.id = t.user_id '
					.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
					.' LEFT JOIN #__ownservice_branches AS b ON b.id = p.branch_id '
					.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
					.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id = t.rate_id '
					.' LEFT JOIN #__ownservice_currencies AS c ON c.id = r.currency_id '
					.$where
					.' GROUP BY t.id '
					.$orderby
					;

		$db->setQuery($query);
		$rows = $db->loadObjectList();

		return count($rows);
	}

	function getTotalCorrectTransactions()
	{
		global $mainframe, $option;

		//Initialise variables
		$user			= & JFactory::getUser();
		$ownuser		= & Ownservice::getUser();
		$db				= & JFactory::getDbo();

		//get vavrs
		$gid			= $user->gid;
		$level			= $ownuser->get('level_id');

		//filter 
		$filter_branch		= $mainframe->getUserStateFromRequest($option.'.correctedtransaction.filter_branch','filter_branch',0,'int' );
		$filter_order		= $mainframe->getUserStateFromRequest($option.'.correctedtransaction.filter_order', 'filter_order', 't.invoice', 'cmd');;
		$filter_order_dir	= $mainframe->getUserStateFromRequest($option.'.correctedtransaction.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');
		$search				= $mainframe->getUserStateFromRequest($option.'.correctedtransaction.search', 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$where			= array();
		$where[]		= 't.state = '.$db->Quote('6');
		

		//check access 
		if( ($gid < 25) && ($level < 5) ) {
			if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$ownuser->get('branch_id');
			}
		} else {
			//filter branch
			if($filter_branch)
			{
				$where[] = 't.branch_id='.$filter_branch;	
			}
		}
		
		//filter search
		if($search) {
			$where[]  =  ' ( t.invoice LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).' OR '
						.' t.account LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).')';
		}

		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_dir.', t.created';

		//Query to retrieve all categories that belong under the web links saction and that are published.
		$query = ' SELECT t.*,  '
					.' u.username, p.id AS memberid, a.bank_name, a.branch_city, '
					.' c.iscore '
					.' FROM #__ownservice_transactions AS t '
					.' LEFT JOIN #__users AS u ON u.id = t.user_id '
					.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
					.' LEFT JOIN #__ownservice_branches AS b ON b.id = p.branch_id '
					.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
					.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id = t.rate_id '
					.' LEFT JOIN #__ownservice_currencies AS c ON c.id = r.currency_id '
					.$where
					.' GROUP BY t.id '
					.$orderby
					;
					
		$db->setQuery($query);
		$rows = $db->loadObjectList();

		return count($rows);
	}

	function getTotalRequestApproval()
	{
		global $mainframe, $option;

		//Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$db			= & JFactory::getDbo();

		//get vars
		$gid		= $user->gid;
		$level		= $ownuser->get('level_id');
		
		$filter_branch	= $mainframe->getUserStateFromRequest($option.'.requestapproval.filter_branch','filter_branch',0,'int' );
		$filter_order		= $mainframe->getUserStateFromRequest($option.'.requestapproval.filter_order', 'filter_order', 't.requested_date', 'cmd');
		$filter_order_Dir	= $mainframe->getUserStateFromRequest($option.'.requestapproval.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');
		$search			= $mainframe->getUserStateFromRequest($option.'.requestapproval.search', 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$where		= array();
		$where[]	= 't.state = 2';

		//check access 
		if( ($gid < 25) &&  ($level < 5) ) {
			if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$ownuser->get('branch_id');
			}
		} else {
			//filter branch
			if($filter_branch) {
				$where[] = 't.branch_id ='.$filter_branch;
			}
		}
		
		//filter search
		if($search) {
			$where[]  =  ' ( t.invoice LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).' OR '
						.' t.account LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).')';
		}
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_dir.', t.created';
		
		//Query to retrieve all categories that belong under the web links saction and that are published.
		$query = ' SELECT t.*, p.id AS memberid, p.birthday, u.name, u.username, b.name AS branchname, '
				.' b.code AS branchcode, c.iscore, a.bank_name AS abankname, a.branch_name AS abranchname, a.branch_city '
				.' FROM #__ownservice_transactions AS t '
				.' LEFT JOIN #__users AS u ON u.id = t.user_id '
				.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
				.' LEFT JOIN #__ownservice_accounts AS a ON a.user_id = u.id '
				.' LEFT JOIN #__ownservice_branches AS b ON b.id = p.branch_id '
				.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id = t.rate_id '
				.' LEFT JOIN #__ownservice_currencies AS c ON c.id = r.currency_id '
				.$where
				.' GROUP BY t.id '
				.$orderby;
		
		$db->setQuery($query);
		$rows = $db->loadObjectList();

		return count($rows);
	}

	/**
	 * Method to get Total Request Approval Corrected
	 *
	 *
	 * @return int
	 * @since 1.0
	*/
	function getTotalRequestapprovalcorrected()
	{
		global $mainframe, $option;

		//Initialise variables
		$user	= & JFactory::getUser();
		$ownuser= & Ownservice::getUser();
		$db		= & JFactory::getDbo();

		//get vavrs
		$gid	= $user->gid;
		$level	= $ownuser->get('level_id');

		//filter 
		$filter_branch		= $mainframe->getUserStateFromRequest($option.'.requestapprovalcorrected.filter_branch','filter_branch',0,'int' );
		$filter_order		= $mainframe->getUserStateFromRequest($option.'.requestapprovalcorrected.filter_order', 'filter_order', 't.invoice', 'cmd');;
		$filter_order_dir	= $mainframe->getUserStateFromRequest($option.'.requestapprovalcorrected.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');
		$search				= $mainframe->getUserStateFromRequest($option.'.requestapprovalcorrected.search', 'search', '', 'string' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		$where	= array();
		$where[] = 't.state = '.$db->Quote('7');
		
		//check access 
		if( ($gid < 25) && ($level < 5) ) {
			if( $ownuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$ownuser->get('branch_id');
			}
		} else {
			//branch
			if($filter_branch)
			{
				$where[] = 't.branch_id='.$filter_branch;	
			}
		}

		//filter search
		if($search) {
			$where[]  =  ' ( t.invoice LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).' OR '
						.' t.account LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ).')';
		}
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_dir.', t.created';

		//Query to retrieve all categories that belong under the web links saction and that are published.
		$query = ' SELECT t.*,  '
					.' u.username, p.id AS memberid, a.bank_name, a.branch_city, '
					.' c.iscore '
					.' FROM #__ownservice_transactions AS t '
					.' LEFT JOIN #__users AS u ON u.id = t.user_id '
					.' LEFT JOIN #__ownservice_users AS p ON p.user_id = u.id '
					.' LEFT JOIN #__ownservice_branches AS b ON b.id = p.branch_id '
					.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
					.' LEFT JOIN #__ownservice_exchangerates AS r ON r.id = t.rate_id '
					.' LEFT JOIN #__ownservice_currencies AS c ON c.id = r.currency_id '
					.$where
					.' GROUP BY t.id '
					.$orderby
					;

		$db->setQuery($query);
		$rows = $db->loadObjectList();

		return count($rows);
	}
}
?>